package com.pym.common;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;

public class Pagination<T> {
    /**
     * 所在页数
     * */
    private int indexNO;
    /**
     * 总数据页数
     * */
    private int pageCount;
    /**
     * 每页显示行数
     * */
    private int rows = 10;
    /**
     * 请求数据
     * */
    @JsonIgnore
    private Object requestData;
    /**
     * 数据集合
     * */
    private List<T> dataList = new ArrayList<T>();
    /**
     * 数据总条数
     */
    private int count;
    /**
     * 是否出错
     * */
    private boolean hasError = false;
    /**
     * 是否已登陆
     */
    private boolean hasLogin = true;
    /**
     * 出错信息
     * */
    private String error;

    public Pagination(int pageCount, List<T> dataList, Integer indexNO) {
        setPageCount(pageCount);
        this.dataList = dataList;
        if(indexNO != null) {
            this.indexNO = indexNO;
        }
    }


    public Pagination(int pageCount, List<T> dataList, Integer indexNO, Integer row) {
        // 设置起始页码
        if(indexNO != null) {
            this.indexNO = indexNO;
        }
        // 设置显示行数
        if(row != null) {
            this.rows = row;
        }
        setPageCount(pageCount);
        this.dataList = dataList;
    }

    public Pagination() {
    }

    public int getIndexNO() {
        return indexNO;
    }
    public void setIndexNO(int indexNO) {
        this.indexNO = indexNO;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public int getRows() {
        return rows;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }
    @JsonProperty(value="rows")
    public List<T> getDataList() {
        return dataList;
    }
    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }
    public Object getRequestData() {
        return requestData;
    }
    public void setRequestData(Object requestData) {
        this.requestData = requestData;
    }
    public boolean isHasError() {
        return hasError;
    }
    public void setHasError(boolean hasError) {
        this.hasError = hasError;
    }
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
    public int getPageCount() {
        return pageCount;
    }
    /**
     * 要在set rows之后。
     * 才能生效。
     * @param count 为数据总数，而非页数
     */
    public void setPageCount(int count) {
        this.count = count;
        this.pageCount = (int)Math.ceil(count/(double)rows);
    }
    public boolean isHasLogin() {
        return hasLogin;
    }
    public void setHasLogin(boolean hasLogin) {
        this.hasLogin = hasLogin;
    }

}
